# Maintainer: Stephen Groat <stephen@egroat.com>

_realname=cmocka
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.5
pkgrel=1
pkgdesc="Unit testing framework for C (mingw-w64)"
arch=('any')
license=('Apache')
url="https://cmocka.org/"
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
source=("https://cmocka.org/files/1.1/${_realname}-${pkgver}.tar.xz"
        "001-install-fixes.patch"
        "002-dll-name.patch")
sha256sums=('f0ccd8242d55e2fd74b16ba518359151f6f8383ff8aef4976e48393f77bba8b6'
            'd0ce41c681a87741883d801ef2f1d79c7a630fe974543ae001d29bfc1aea1925'
            'b224f3906cdd3c90fd6e08e51f9d26bc0e7160f3d78a0e6fc520e9b054f39731')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/001-install-fixes.patch"
  patch -Np1 -i "${srcdir}/002-dll-name.patch"
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DWITH_STATIC_LIB=ON \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DWITH_CMOCKERY_SUPPORT=ON \
    -DUNIT_TESTING=ON \
    ../${_realname}-${pkgver}

  make
}

check() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make test || true
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make install DESTDIR="${pkgdir}"
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
